/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import mygame.Backbone.GlobalVars;
import mygame.Backbone.ICam;
import mygame.Backbone.LightManager;
import mygame.Backbone.PlanetCam;
import mygame.Backbone.SavingHandler;
import mygame.GUI.GUIManager;
import mygame.Input.InputHandler;
import mygame.Model.ModelManager;
import mygame.Model.PlanetManager;
import mygame.Player.Player;
import mygame.Scene.SceneManager;

import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;

/**
 *
 * @author Tobi
 */
public class Manager {

    private int CurrentGameState;
    private static Manager _instance;
    private boolean isRunning;
    private Node root;
    private Node guiNode;
    private AppSettings settings;
    private ICam mycam;
    private Player player;
    private AssetManager asm;
    private SceneManager scm;
    private ModelManager mdm;
    private LightManager ltm;
    private GUIManager guim;
    private InputHandler iph;
    private SavingHandler svh;
    private PlanetManager ptm;
    //physics
    private AppStateManager StateManager;
    private BulletAppState physix;

    public void update(float tpf) {
        if (CurrentGameState == GlobalVars.GAME_MODE_INITIALISING) {
            this.init();
        } else if (CurrentGameState == GlobalVars.GAME_MODE_STARTING_GAME) {
            this.start();
        } else if (CurrentGameState == GlobalVars.GAME_MODE_PLAYING) {
            mycam.update(tpf);
            guim.update();
        }
    }

    public Manager(Node root, AppStateManager StateManager, AssetManager asm, Camera cam, Node guiNode, AppSettings settings, InputHandler iph,ViewPort viewPort,GUIManager guim) {
        this.root = root;
        this.mycam = mycam;
        this.asm = asm;
        this.iph = iph;
        this.settings = settings;

        //attach Scene
        this.scm = new SceneManager(root, asm ,viewPort);
        //attach Objects
        this.mdm = new ModelManager(root, asm);
        //attach Lights
        this.ltm = new LightManager(root,viewPort,asm);
        //attach gui
        this.guim = guim;
        //make savehandler
        this.svh = new SavingHandler();
        //planet manager
        this.ptm = new PlanetManager(root,asm);
        mycam = new PlanetCam(cam, iph.getInputManager(),root);
       
        //attach Physics
        physix = new BulletAppState();
        this.StateManager = StateManager;
        StateManager.attach(physix);
        //Show Physix Shape
        //physix.getPhysicsSpace().enableDebug(asm);

        //make Player
        this.player = new Player();
        //start running
        this.isRunning = true;

        //setMode to Initialise
        setGameMode(GlobalVars.GAME_MODE_INITIALISING);
    }

    public static void MakeInstance(Node root, AppStateManager StateManager, AssetManager asm, Camera cam, Node guiNode, AppSettings settings, InputHandler iph,ViewPort viewPort,GUIManager guim) {
        _instance = new Manager(root, StateManager, asm, cam, guiNode, settings, iph, viewPort,guim);
    }

    public static Manager GetInstance() {
        return _instance;
    }

    /**
     * Initialise some components
     */
    public void init() {
        
        this.ltm.init();
        this.guim.init();
        setGameMode(GlobalVars.GAME_MODE_STARTING_GAME);
    }

    /**
     * initialise components need for the game to run.
     */
    public void start() {
        //init Scene and Model Environment
        this.scm.init();
        this.mdm.init();
        //this.mdm.placePlanet();
        ((PlanetCam)this.mycam).addPlanet(this.ptm.generatePlanet(3));
        this.mycam.init();
        //enable KeyInput
        this.setupGameKeys();

        //set gamemode to enable playing
        setGameMode(GlobalVars.GAME_MODE_PLAYING);
    }

    public BulletAppState getPhysix() {
        return this.physix;
    }

    public ICam getMyCam() {
        return mycam;
    }

    public SceneManager getSceneManager() {
        return scm;
    }

    public ModelManager getModelManager() {
        return mdm;
    }

    public GUIManager getGUIManager(){
    return this.guim;
    }
    public Node getRoot() {
        return this.root;
    }

    public AssetManager getAssetManager() {
        return this.asm;
    }

    public LightManager getLightManager(){
    	return this.ltm;
    }

    public PlanetManager getPlanetManager(){
    	return this.ptm;
    }
    
    public int getGameMode() {
        return this.CurrentGameState;
    }

    public AppSettings getSettings() {
        return this.settings;
    }

    public Player getPlayer() {
        return this.player;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public void StopGame() {
        this.isRunning = false;
    }

    public void setGameMode(int mode) {
        this.CurrentGameState = mode;
    }

    public SavingHandler getSavingHandler() {
        return this.svh;
    }
    
    public void setupGameKeys(){ 
        this.iph.clearMappings();
        this.mycam.registerWithInput();
        this.iph.setupGameKeys();
    }
    public void setupMenuKeys(){
        this.iph.clearMappings();
        this.guim.getMenuHandler().setupGameKeys();
    }
}
